<?php

/*

  Action	$5

  +2 Cards
  Discard any number of cards. +$1 per card discarded.  Each other player may discard 2 cards. If he does, he draws a card.

 */

class Vault extends Card implements iAction, iKingdom, iProsperity {

    public function __construct() {
        parent::__construct("Vault", 5);
        $this->undoPlay = false;
        $this->undoBuy = true;
    }

    public function getDescription() {
        return array(
            "+2 Cards",
            "Discard any number of cards. +$1 per card discarded.  Each other player may discard 2 cards. If he does, he draws a card.");
    }
    
    public function play(Game $game, $isRealCard) {

        $names = $game->getOtherPlayersNames();
        foreach ($names as $name) {
            $otherPlayHand = $game->getPlayHand($name);
            if (sizeof($otherPlayHand) > 0) {

                $options = array(new Option(1, "Yes"), new Option(2, "No"));
                $output = MySocket::GetUserReply($game, Input_ChooseOne, $options, 1, 1, null, 'Discard 2 cards?', $name, true);
            }
        }

        $drawed_cards = $game->drawCards(2);
         ScreenDisplay::addScreenMessage($game, true, "draw " . nicePrint($drawed_cards), "draws " . sizeof($drawed_cards) . " cards");


        $playHand = $game->getPlayHand();
        if (sizeof($playHand) > 0) {

            $output = MySocket::GetUserReply($game, Input_ChooseMany, $playHand, 0, sizeof($playHand), null, "Choose cards to discard");

            foreach ($output as $p) {
                $game->moveCardFromPileToPile($p, PlayHand, DiscardPile);
            }
            $money = sizeof($output);
            $game->addMoney(new Price($money));
             ScreenDisplay::addScreenMessage($game, true, "discard " . nicePrint($output) . " and get +$money$", "discards " . sizeof($output) . " cards and get +$money$");
        }

        $game->setPhase(Phase_Sleep);
        //the waiting function
        $users_replies = MySocket::getUsersReplies($game);
        foreach ($users_replies as $name => $output) {

            $answer = $output[0];

            if ($answer == "Yes") {
                $playHand = $game->getPlayHand($name);

                if (sizeof($playHand) > 2) {

                    $output = MySocket::GetUserReply($game, Input_ChooseMany, $playHand, 2, 2, null, "Discard 2 cards", $name, true);
                } else if (sizeof($playHand) == 2) {
                    $game->discardPlayHand($name);
                    $drawed_cards = $game->drawCards(1, $name);
                     ScreenDisplay::addScreenMessage($game, true, "discard " . nicePrint($playHand) . " and draw " . nicePrint($drawed_cards), "discards " . sizeof($playHand) . " cards and draws " . sizeof($drawed_cards) . " cards", $name);

                    $game->setPhase(Phase_Sleep, $name);
                } else {
                    $drawed_cards = $game->discardPlayHand($name);
                     ScreenDisplay::addScreenMessage($game, true, "discard " . nicePrint($playHand), "discards " . sizeof($playHand) . " cards", $name);
                    $game->setPhase(Phase_Sleep, $name);
                }
            } else if ($answer == "No") {
				ScreenDisplay::addScreenMessage($game, true, "discard nothing", "discards nothing", $name);
                $game->setPhase(Phase_Sleep, $name);
            }
        }

        //the waiting function
        $users_replies = MySocket::getUsersReplies($game);
        foreach ($users_replies as $name => $output) {

            foreach ($output as $card_name) {
                $game->moveCardFromPileToPile($card_name, PlayHand, DiscardPile, $name);
            }
            $drawed_cards = $game->drawCards(1, $name);
             ScreenDisplay::addScreenMessage($game, true, "discard " . nicePrint($output) . " and draw " . nicePrint($drawed_cards), "discards " . sizeof($output) . " cards and draws " . sizeof($drawed_cards) . " cards", $name);

            $game->setPhase(Phase_Sleep, $name);
        }

        $game->cardActionFinished();
        return true;
    }

}

?>